home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / mach-info.cc < prev    next >
C/C++ Source or Header  |  1997-03-07  |  5KB  |  244 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include "f77-fcn.h"
  28. #include "lo-error.h"
  29. #include "mach-info.h"
  30.  
  31. extern "C"
  32. {
  33.   double F77_FCN (d1mach, D1MACH) (const int&);
  34. }
  35.  
  36. oct_mach_info *oct_mach_info::instance = 0;
  37.  
  38. union equiv
  39. {
  40.   double d;
  41.   int i[2];
  42. };
  43.  
  44. struct
  45. float_params
  46. {
  47.   oct_mach_info::float_format fp_fmt;
  48.   equiv fp_par[4];
  49. };
  50.  
  51. #define INIT_FLT_PAR(fp, fmt, sm1, sm2, lrg1, lrg2, rt1, rt2, dv1, dv2) \
  52.   do \
  53.     { \
  54.       fp.fp_fmt = (fmt); \
  55.       fp.fp_par[0].i[0] = (sm1);  fp.fp_par[0].i[1] = (sm2); \
  56.       fp.fp_par[1].i[0] = (lrg1); fp.fp_par[1].i[1] = (lrg2); \
  57.       fp.fp_par[2].i[0] = (rt1);  fp.fp_par[2].i[1] = (rt2); \
  58.       fp.fp_par[3].i[0] = (dv1);  fp.fp_par[3].i[1] = (dv2); \
  59.     } \
  60.   while (0)
  61.  
  62. static int
  63. equiv_compare (const equiv *std, const equiv *v, int len)
  64. {
  65.   int i;
  66.   for (i = 0; i < len; i++)
  67.     if (v[i].i[0] != std[i].i[0] || v[i].i[1] != std[i].i[1])
  68.       return 0;
  69.   return 1;
  70. }
  71.  
  72. void
  73. oct_mach_info::init_float_format (void)
  74. {
  75.   float_params fp[5];
  76.  
  77.   INIT_FLT_PAR (fp[0], oct_mach_info::ieee_big_endian,
  78.            1048576,  0,
  79.         2146435071, -1,
  80.         1017118720,  0,
  81.         1018167296,  0);
  82.  
  83.   INIT_FLT_PAR (fp[1], oct_mach_info::ieee_little_endian,
  84.          0,    1048576,
  85.         -1, 2146435071,
  86.          0, 1017118720,
  87.          0, 1018167296);
  88.  
  89.   INIT_FLT_PAR (fp[2], oct_mach_info::vax_d,
  90.            128,  0,
  91.         -32769, -1,
  92.           9344,  0,
  93.           9344,  0);
  94.  
  95.   INIT_FLT_PAR (fp[3], oct_mach_info::vax_g,
  96.             16,  0,
  97.         -32769, -1,
  98.          15552,  0,
  99.          15552,  0);
  100.  
  101.   INIT_FLT_PAR (fp[4], oct_mach_info::unknown,
  102.         0, 0,
  103.         0, 0,
  104.         0, 0,
  105.         0, 0);
  106.  
  107.   equiv mach_fp_par[4];
  108.  
  109.   mach_fp_par[0].d = F77_FCN (d1mach, D1MACH) (1);
  110.   mach_fp_par[1].d = F77_FCN (d1mach, D1MACH) (2);
  111.   mach_fp_par[2].d = F77_FCN (d1mach, D1MACH) (3);
  112.   mach_fp_par[3].d = F77_FCN (d1mach, D1MACH) (4);
  113.  
  114.   int i = 0;
  115.   do
  116.     {
  117.       if (equiv_compare (fp[i].fp_par, mach_fp_par, 4))
  118.     {
  119.       native_float_fmt = fp[i].fp_fmt;
  120.       break;
  121.     }
  122.     }
  123.   while (fp[++i].fp_fmt != oct_mach_info::unknown);
  124. }
  125.  
  126. void
  127. oct_mach_info::ten_little_endians (void)
  128. {
  129.   // Are we little or big endian?  From Harbison & Steele.
  130.  
  131.   union
  132.   {
  133.     long l;
  134.     char c[sizeof (long)];
  135.   } u;
  136.  
  137.   u.l = 1;
  138.  
  139.   big_chief = (u.c[sizeof (long) - 1] == 1);
  140. }
  141.  
  142. oct_mach_info::oct_mach_info (void)
  143. {
  144.   init_float_format ();
  145.   ten_little_endians ();
  146. }
  147.  
  148. oct_mach_info::float_format
  149. oct_mach_info::native_float_format (void)
  150. {
  151.   if (! instance)
  152.     instance = new oct_mach_info ();
  153.  
  154.   return instance->native_float_fmt;
  155. }
  156.  
  157. bool
  158. oct_mach_info::words_big_endian (void)
  159. {
  160.   if (! instance)
  161.     instance = new oct_mach_info ();
  162.  
  163.   return instance->big_chief;
  164. }
  165.  
  166. bool
  167. oct_mach_info::words_little_endian (void)
  168. {
  169.   if (! instance)
  170.     instance = new oct_mach_info ();
  171.  
  172.   return ! instance->big_chief;
  173. }
  174.  
  175. oct_mach_info::float_format
  176. oct_mach_info::string_to_float_format (const string& s)
  177. {
  178.   oct_mach_info::float_format retval = oct_mach_info::unknown;
  179.  
  180.   if (s == "native" || s == "n")
  181.     retval = oct_mach_info::native;
  182.   else if (s == "ieee-be" || s == "b")
  183.     retval = oct_mach_info::ieee_big_endian;
  184.   else if (s == "ieee-le" || s == "l")
  185.     retval = oct_mach_info::ieee_little_endian;
  186.   else if (s == "vaxd" || s == "d")
  187.     retval = oct_mach_info::vax_d;
  188.   else if (s == "vaxg" || s == "g")
  189.     retval = oct_mach_info::vax_g;
  190.   else if (s == "cray" || s == "c")
  191.     retval = oct_mach_info::cray;
  192.   else if (s == "unknown")
  193.     retval = oct_mach_info::unknown;
  194.   else
  195.     (*current_liboctave_error_handler)
  196.       ("invalid architecture type specified");
  197.  
  198.   return retval;
  199. }
  200.  
  201. string
  202. oct_mach_info::float_format_as_string (float_format flt_fmt)
  203. {
  204.   string retval = "unknown";
  205.  
  206.   switch (flt_fmt)
  207.     {
  208.     case native:
  209.       retval = "native";
  210.       break;
  211.  
  212.     case ieee_big_endian:
  213.       retval = "ieee_big_endian";
  214.       break;
  215.  
  216.     case ieee_little_endian:
  217.       retval = "ieee_little_endian";
  218.       break;
  219.  
  220.     case vax_d:
  221.       retval = "vax_d_float";
  222.       break;
  223.  
  224.     case vax_g:
  225.       retval = "vax_g_float";
  226.       break;
  227.  
  228.     case cray:
  229.       retval = "cray";
  230.       break;
  231.  
  232.     default:
  233.       break;
  234.     }
  235.  
  236.   return retval;
  237. }
  238.  
  239. /*
  240. ;;; Local Variables: ***
  241. ;;; mode: C++ ***
  242. ;;; End: ***
  243. */
  244.